package org.example.deepseekproject_back.exception;

import lombok.extern.slf4j.Slf4j;
import org.example.deepseekproject_back.common.BaseResponse;
import org.example.deepseekproject_back.common.ResultUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * TODO:捕获并处理异常
 * @author Lenovo
 * 全局异常处理器
 * 这是一个异常处理类，
 * 它的核心作用是捕获并处理整个应用程序中抛出的异常，然后返回一个友好的、格式统一的错误响应给前端。它是异常处理的“终点站”
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {



    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException e) {
        log.error("BusinessException", e);
        return ResultUtils.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("RuntimeException", e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
    }
}